package com.org.manhu.chargehelper.ui;

import android.app.Activity;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.org.manhu.chargehelper.ChargeApplication;
import com.org.manhu.chargehelper.R;
import com.org.manhu.chargehelper.Zxing.CaptureActivity;
import com.org.manhu.chargehelper.broadcastreceiver.UpdateMoneyReceiver;
import com.org.manhu.chargehelper.broadcastreceiver.UpdateNickNameReceiver;
import com.org.manhu.chargehelper.constant.Constant;
import com.org.manhu.chargehelper.custom.BatteryCharger;
import com.org.manhu.chargehelper.tools.AppManager;
import com.org.manhu.chargehelper.tools.DeviceInfo;
import com.org.manhu.chargehelper.tools.SharedPreferncesUtil;

/**
 * @文件名：ScanActivity.java
 * @包名：com.org.manhu.chargehelper.ui
 * @作者：李朋辉 @QQ：892839211
 * @创建时间：2015年2月5日 下午7:02:40
 *
 * @描述：
 */
public class ScanActivity extends Activity implements OnClickListener {

	private ImageView scanIv, backIv, userInfoMoreIv;
	private Button exchangeBtn, cashOutBtn;

	private TextView userNameTv, awardNumTv;
	private BatteryCharger battery;
	private UpdateNickNameReceiver mReceiver;
	private UpdateMoneyReceiver receivers;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_scan);
		AppManager.getAppManager().addActivity(this);

		scanIv = (ImageView) findViewById(R.id.scan_barcode_image);
		battery = (BatteryCharger) findViewById(R.id.battery_charge);
		userNameTv = (TextView) findViewById(R.id.user_name);
		awardNumTv = (TextView) findViewById(R.id.user_award_num);
		userNameTv.setText(SharedPreferncesUtil.getNickName(this));
		awardNumTv.setText(SharedPreferncesUtil.getMoney(this) + "");
		backIv = (ImageView) findViewById(R.id.title_back);
		userInfoMoreIv = (ImageView) findViewById(R.id.user_info_more);
		exchangeBtn = (Button) findViewById(R.id.exchange_btn);
		cashOutBtn = (Button) findViewById(R.id.tixian_btn);
		cashOutBtn.setVisibility(View.GONE);

		if (ChargeApplication.isBoxConnected) {
			battery.setVisibility(View.VISIBLE);
			battery.startChargerAnim();
		}

		backIv.setOnClickListener(this);
		exchangeBtn.setOnClickListener(this);
		cashOutBtn.setOnClickListener(this);
		scanIv.setOnClickListener(this);
		userInfoMoreIv.setOnClickListener(this);
		userNameTv.setOnClickListener(this);

		mReceiver = new UpdateNickNameReceiver(userNameTv);
		IntentFilter filter = new IntentFilter();
		filter.addAction(Constant.ACTION_UPDATE_NICKNAME);
		registerReceiver(mReceiver, filter);

		receivers = new UpdateMoneyReceiver(awardNumTv);
		IntentFilter filters = new IntentFilter();
		filter.addAction(Constant.ACTION_UPDATE_MONEY);
		registerReceiver(receivers, filters);
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		awardNumTv.setText(SharedPreferncesUtil.getMoney(this)+"");
		userNameTv.setText(SharedPreferncesUtil.getNickName(this));
	}
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		unregisterReceiver(mReceiver);
		unregisterReceiver(receivers);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.scan_barcode_image:
			Intent intent = new Intent(this, CaptureActivity.class);
			startActivity(intent);
			break;
		case R.id.title_back:
			finish();
			break;
		case R.id.exchange_btn:
			break;
		case R.id.tixian_btn:
			Intent it = new Intent(this, CashOutActivity.class);
			startActivity(it);
			break;
		case R.id.user_info_more:
			Intent its = new Intent(this, CenterActivity.class);
			startActivity(its);
			break;
		case R.id.user_name:
			Intent ite = new Intent(this, ModifyNickNameActivity.class);
			startActivity(ite);
			break;
		}
	}

}
